﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocketIOClient;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
using SocketIOClient.Messages;

namespace LittleMonkey.Communication
{
    public class websocketclient
    {

        private Client socket;
        public websocketclient()
        {


        }

        public void initialize(string connectionstring)
        {
            socket = new Client(connectionstring);
            socket.Opened += SocketOpened;
            socket.Message += SocketMessage;
            socket.SocketConnectionClosed += SocketConnectionClosed;
            socket.Error += SocketError;

            socket.On("connect", (fn) =>
            {
                Console.WriteLine("\r\nConnected event...\r\n");
                Console.WriteLine("Emit Part object");
                //Part newPart = new Part() { PartNumber = "K4P2G324EC", Code = "DDR2", Level = 1 };
               // socket.Emit("partInfo", newPart);

            });

            socket.On("store", (data) =>
            {
                //Console.WriteLine("  raw message:      {0}", data.RawMessage);
                //Console.WriteLine("  string message:   {0}", data.MessageText);
                //Console.WriteLine("  json data string: {0}", data.Json.ToJsonString());
                //Console.WriteLine("  json raw:         {0}", data.Json.Args[0]);

                // cast message as Part - use type cast helper
                //Part part = data.Json.GetFirstArgAs<Part>();
               // Console.WriteLine(" Part Level:   {0}\r\n", part.Level);
            });


        }

        void SocketOpened(object sender, EventArgs e)
        {
            Console.WriteLine("socket client Connected");
        }

        void SocketError(object sender, ErrorEventArgs e)
        {
            Console.WriteLine("socket client error:");
            Console.WriteLine(e.Message);
        }

        void SocketConnectionClosed(object sender, EventArgs e)
        {
            Console.WriteLine("WebSocketConnection was terminated!");
        }

        void SocketMessage(object sender, MessageEventArgs e)
        {
            
            if (string.IsNullOrEmpty(e.Message.Event))
                Console.WriteLine("Generic SocketMessage: {0}", e.Message.MessageText);
            else
                Console.WriteLine("Generic SocketMessage: {0} : {1}", e.Message.Event, e.Message.JsonEncodedMessage.ToJsonString());
        }
        public void Close()
        {
            if (this.socket != null)
            {
                socket.Opened -= SocketOpened;
                socket.Message -= SocketMessage;
                socket.SocketConnectionClosed -= SocketConnectionClosed;
                socket.Error -= SocketError;
                this.socket.Dispose(); 
            }
        }

        public void sendmessage(string evt , string message)
        {

        }


         
    }
}
